package com.galen.manual.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * MyBatis Plus 元数据处理器 - 简化版本
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MyMetaObjectHandler.class);

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("执行插入填充操作");
        // 设置创建时间和更新时间
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());

        // 尝试调用实体的preInsert方法
        Object originalObject = metaObject.getOriginalObject();
        if (originalObject instanceof com.galen.manual.entity.BaseEntity) {
            ((com.galen.manual.entity.BaseEntity) originalObject).preInsert();
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("执行更新填充操作");
        // 设置更新时间
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());

        // 尝试调用实体的preUpdate方法
        Object originalObject = metaObject.getOriginalObject();
        if (originalObject instanceof com.galen.manual.entity.BaseEntity) {
            ((com.galen.manual.entity.BaseEntity) originalObject).preUpdate();
        }
    }
}